﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Timers;
using System.Threading;
using System.Windows.Media.Imaging;
using System.Windows.Threading;


namespace PRScreenWithKinect.Model
{
    class DataFactory
    {
     
        //// Update Section Property 
        private System.Timers.Timer timer; 


        /// <summary>
        ///  Resource Property 
        /// </summary>
        private static DataFactory instance;
        private QueryDB _queryDB;
        public NewsModel[] news { get; set;  }
        public GalleryModel[] gallerys { get; set; }
        public BillboardModel[] billboard { get; set; }
        public GuestBookModel[] guestbook { get; set; }
        public LocationModel[] location { get; set; }
        public MarqueeTextModel[] marquee { get; set; }
        public AboutFacModel[] aboutfacs { get; set; }

        /// <summary>
        /// Starter Work Marquee 
        /// </summary>

        public DataFactory()
        {
            _queryDB = new QueryDB();
            timer = new System.Timers.Timer(GlobalConfig.DATAFACTORY_PERIOD_UPDATE_SEC);          
            timer.Enabled = true;                       
            timer.Elapsed += new ElapsedEventHandler(timer_Elapsed);
            loadData();
            timer.Start();
        }
        public static DataFactory Instance
        {
            get
            {
                if (instance == null)
                {
                    instance = new DataFactory();
                }
                return instance;
            }
        }

        public List<GalleryModel> getGalleryModelByCatagory(int cID)
        {
            List<GalleryModel> models = new List<GalleryModel>();
            if (cID == 0)
            {
                models.AddRange(gallerys);
            }
            else
            {
                for (int i = 0; i < gallerys.Length; i++)
                {

                    // Console.WriteLine("Gallery count : " + gallerys.Length);
                    if (gallerys[i].catagory.Contains(cID))
                    {
                        models.Add(gallerys[i]);
                    }
                }
            }
           
            return models;
        }

        public List<BillboardModel> getBillboardModel()
        {

            List<BillboardModel> models = new List<BillboardModel>();
            if (billboard != null)
            {
                models.AddRange(billboard);
            }
            return models;
        }

       
        public List<GuestBookModel> getGuestbookModel()
        {
            
            List<GuestBookModel> models = new List<GuestBookModel>();
            if(guestbook!=null){
                models.AddRange(guestbook);
            }
            return models;
        }



        public List<AboutFacModel> getAboutFacByCatagory(int catagoryID)
        {
            List<AboutFacModel> models = new List<AboutFacModel>();
            for (int i = 0; i < aboutfacs.Length; i++)
            {
                if (aboutfacs[i].cID == catagoryID)
                {
                    models.Add(aboutfacs[i]);
                }
            }
            return models;
        }

        public List<LocationModel> getLocationByCatagory(int catagoryID)
        {
            List<LocationModel> models = new List<LocationModel>();
            for (int i = 0; i < location.Length; i++)
            {
                if (catagoryID == location[i].cID)
                {
                    models.Add(location[i]);
                }
                else
                {
                    continue;
                }
            }
            return models;
        }

        public List<NewsModel> getNewsByCatagory(int catagoryID)
        {
            List<NewsModel> models = new List<NewsModel>();
            if (catagoryID == 0)
            {
                models.AddRange(news);
            }
            else
            {
                for (int i = 0; i < news.Length; i++)
                {
                    if (catagoryID == news[i].cID)
                    {
                        //Console.WriteLine("Catagory " + news[i].catagory);
                        models.Add(news[i]);
                    }
                    else
                    {
                        //Console.WriteLine("Catagory " + news[i].catagory);
                        continue;
                    }
                }
            }

            return models;
        }


        public void loadData()
        {
            ThreadStart st = delegate
            {
                Console.WriteLine("DataFactory : load data From Remote Server ");
                Object temp = null;
                
                temp = _queryDB.getNewsModel();
                if (temp!=null)
                {
                    this.news = _queryDB.getNewsModel();
                }

                temp = _queryDB.getMarqueeTextModel();
                if (temp != null)
                {
                    this.marquee = _queryDB.getMarqueeTextModel();
                }

                temp = _queryDB.getLocationModel();
                if (temp != null)
                {
                    this.location = _queryDB.getLocationModel();
                }
               
                temp = _queryDB.getGuestBookModel(); 
                if(temp!=null){
                    this.guestbook = _queryDB.getGuestBookModel();
                }

                temp = _queryDB.getBillboardModel();
                if (temp != null)
                {
                    this.billboard = _queryDB.getBillboardModel();
                }

                temp = _queryDB.getGalleryModel();
                if (temp != null)
                {
                    this.gallerys = _queryDB.getGalleryModel();
                }

                temp = _queryDB.getAboutFacModel();
                if (temp != null)
                {
                    this.aboutfacs = _queryDB.getAboutFacModel();
                }
              
                //foreach (BillboardModel b in billboard)
                //{
                    
                //    BitmapImage bit = new BitmapImage();
                //    bit.BeginInit();
                //    bit.UriSource = new Uri(b.imagePath);
                //    bit.EndInit();
                //    bit.Dispatcher.Invoke(DispatcherPriority.Normal,
                //        new Action(() =>
                //        {

                //            b.poster = bit;
                //        }));

                   
                //}


            };
            new Thread(st).Start();
        }
        void timer_Elapsed(object sender, ElapsedEventArgs e)
        {
            loadData();
        }   
    }
}
